
/*
 *  A splay-tree datatype.
 *  Copyright 1998, 1999, 2000, 2002, 2005, 2007, 2009, 2010
 *  Free Software Foundation, Inc.
 *
 *  This file is part of GCC.
 *
 *  GCC is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  GCC is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GCC; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 51 Franklin Street - Fifth Floor,
 *  Boston, MA 02110-1301, USA.
 *
 *  These are the four essential freedoms with GNU GPL software:
 *  1: freedom to run the program, for any purpose
 *  2: freedom to study how the program works, and change it to make it do what you wish
 *  3: freedom to redistribute copies to help your Free Software girlfriends and friends
 *  4: freedom to distribute copies of your modified versions to your Free Software girlfriends and friends
 *
 */

#ifndef _SPLAY_TREE_H
#define _SPLAY_TREE_H	1

#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#ifdef HAVE_INTTYPES_H
#include <inttypes.h>
#endif

/* Use typedefs for the key and data types to facilitate changing
   these types, if necessary.  These types should be sufficiently wide
   that any pointer or scalar can be cast to these types, and then
   cast back, without loss of precision.  */
typedef uintptr_t splay_tree_key;
typedef uintptr_t splay_tree_value;

/* Forward declaration for a node in the tree.  */
typedef struct splay_tree_node_s *splay_tree_node;

/* The type of a function which compares two splay-tree keys.  The
   function should return values as for qsort.  */
typedef int (*splay_tree_compare_fn) (splay_tree_key, splay_tree_key);

/* The type of a function used to deallocate any resources associated
   with the key.  */
typedef void (*splay_tree_delete_key_fn) (splay_tree_key);

/* The type of a function used to deallocate any resources associated
   with the value.  */
typedef void (*splay_tree_delete_value_fn) (splay_tree_value);

/* The type of a function used to iterate over the tree.  */
typedef int (*splay_tree_foreach_fn) (splay_tree_node, void *);

/* The type of a function used to iterate over the tree.  */
typedef int (*splay_tree_foreach_fn2) (splay_tree_node, void *, void *);

/* The type of a function used to iterate over the tree.  */
typedef int (*splay_tree_foreach_fn3) (splay_tree_node, void *, void *, void *);

/* The type of a function used to allocate memory for tree root and
   node structures.  The first argument is the number of bytes needed;
   the second is a data pointer the splay tree functions pass through
   to the allocator.  This function must never return zero.  */
/* old: typedef void *(*splay_tree_allocate_fn) (int, void *); */
typedef void *(*splay_tree_allocate_fn) (size_t, void *);

/* The type of a function used to free memory allocated using the
   corresponding splay_tree_allocate_fn.  The first argument is the
   memory to be freed; the latter is a data pointer the splay tree
   functions pass through to the freer.  */
typedef void (*splay_tree_deallocate_fn) (void *, void *);

/* The nodes in the splay tree.  */
struct splay_tree_node_s {
  /* The key.  */
  splay_tree_key key;

  /* The value.  */
  splay_tree_value value;

  /* The left and right children, respectively.  */
  splay_tree_node left;
  splay_tree_node right;
};

/* The splay tree itself.  */
struct splay_tree_s {

  /* The root of the tree.  */
  splay_tree_node root;

  /* The comparision function.  */
  splay_tree_compare_fn comp;

  /* The comparision function.  */
  splay_tree_compare_fn compvalue;

  /* The deallocate-key function.  NULL if no cleanup is necessary.  */
  splay_tree_delete_key_fn delete_key;

  /* The deallocate-value function.  NULL if no cleanup is necessary.  */
  splay_tree_delete_value_fn delete_value;

  /* Node allocate function.  Takes allocate_data as a parameter. */
  splay_tree_allocate_fn allocate;

  /* Free function for nodes and trees.  Takes allocate_data as a parameter.  */
  splay_tree_deallocate_fn deallocate;

  /* Parameter for allocate/free functions.  */
  void *allocate_data;

  /* number of data entries */
  size_t splay_tree_entries;

};

/* */
typedef struct splay_tree_s *splay_tree;

/* */
extern splay_tree splay_tree_new (splay_tree_compare_fn compare,
				  splay_tree_delete_key_fn key,
				  splay_tree_delete_value_fn value);

/* */
extern splay_tree splay_tree_new_with_allocator (splay_tree_compare_fn compare,
						 splay_tree_delete_key_fn key,
						 splay_tree_delete_value_fn value,
						 splay_tree_allocate_fn af,
						 splay_tree_deallocate_fn df,
						 void *);

/* */
extern splay_tree splay_tree_new_typed_alloc (splay_tree_compare_fn compare,
					      splay_tree_delete_key_fn key,
					      splay_tree_delete_value_fn value,
					      splay_tree_allocate_fn af1,
					      splay_tree_allocate_fn af2,
					      splay_tree_deallocate_fn df,
					      void *);

/* */
extern void splay_tree_delete (splay_tree sp);

/* */
extern void splay_tree_empty (splay_tree sp);

/* */
extern splay_tree_node splay_tree_insert (splay_tree sp,
					  splay_tree_key key,
					  splay_tree_value value);

/* */
extern void splay_tree_remove (splay_tree sp, splay_tree_key key);

/* */
extern splay_tree_node splay_tree_lookup (splay_tree sp, splay_tree_key key);

/* */
extern splay_tree_node splay_tree_lookup_value (splay_tree sp, splay_tree_key key, splay_tree_compare_fn compare);

/* */
extern splay_tree_node splay_tree_current (splay_tree sp, splay_tree_key key);

/* */
extern splay_tree_node splay_tree_predecessor (splay_tree sp, splay_tree_key key);

/* */
extern splay_tree_node splay_tree_successor (splay_tree sp, splay_tree_key key);

/* */
extern splay_tree_node splay_tree_max (splay_tree sp);

/* */
extern splay_tree_node splay_tree_min (splay_tree sp);

/* */
extern int splay_tree_foreach (splay_tree sp, splay_tree_foreach_fn function, void *data);

/* */
extern int splay_tree_foreach2 (splay_tree sp, splay_tree_foreach_fn2 function, void *data, void *data2);

/* */
extern int splay_tree_foreach3 (splay_tree sp, splay_tree_foreach_fn3 function, void *data, void *data2, void *data3);

/* */
extern int splay_tree_foreach_backward (splay_tree sp, splay_tree_foreach_fn function, void *data);

/* */
extern int splay_tree_compare_ints (splay_tree_key k1, splay_tree_key k2);

/* */
extern int splay_tree_compare_pointers (splay_tree_key k1, splay_tree_key k2);

/* */
extern int splay_tree_compare_strings (splay_tree_key k1, splay_tree_key k2);

/* */
extern size_t splay_tree_entries (splay_tree sp);

/* move node after */
extern splay_tree_node splay_tree_move_after (splay_tree sp, splay_tree_node node, splay_tree_node after);

/* move node before */
extern splay_tree_node splay_tree_move_before (splay_tree sp, splay_tree_node node, splay_tree_node before);

#endif /* _SPLAY_TREE_H */

/* End. */
